
#include <string>
#include "USS.h"
#include "USSclient.h"

using namespace TOB;

US::US(Nameserver::IP nsip): NSRequired(nsip) {

  // Get host
  std::string address;
  Nameserver::convertIP(nsip, address);
  Nameserver ns(address);
  Nameserver::IP usip;

  connected = false;
  if(ns.requestAddress(Server::UserStorage, usip) != Code::Okay) {
    return;
  } 

  Nameserver::convertIP(usip, address);
  client = clnt_create (address.c_str(), USS, USS_VERSION, "udp");

  if (client == NULL) {
    clnt_pcreateerror (address.c_str());
    return;
  };
  connected = true;
};

US::~US() {
  if(client != NULL && connected) {
    clnt_destroy(client);
  }
}

Code::Type US::getUintByLogin (std::string login, std::string field, int& value){
  if(!connected) { return Code::US_DBDoesntRespond; }
         
      getUintParameters  getUint;
      strcpy(getUint.login, login.c_str()); 
      strcpy(getUint.field, field.c_str());     
      getUint.value=value;

      getUintResult *result;
      result=getuintbylogin_1(&getUint, client);
      if(!result) { return Code::US_DBDoesntRespond; }
      value=result->result;
      return (Code::Type)result->code;
};


Code::Type US::getStringByLogin (std::string login, std::string field, std::string& value){
  if(!connected) { return Code::US_DBDoesntRespond; }
   
      getParameters get;
      strcpy(get.login, login.c_str());
      strcpy(get.field, field.c_str());      
      strcpy(get.value, value.c_str());

      getResult *result;
      result = getstringbylogin_1(&get, client);
      if(!result) { return Code::US_DBDoesntRespond; }
      value=result->result;
      return (Code::Type)result->code;
      
};

Code::Type US::existenceLogin (std::string login, int& value){
  if(!connected) { return Code::US_DBDoesntRespond; }
         
      existenceParameters existence;
      strcpy(existence.login, login.c_str());      
      existence.value[1]=value;

      existenceResult *result;
      result=existencelogin_1(&existence, client);
      if(!result) { return Code::US_DBDoesntRespond; }
      value=result->result;
      return (Code::Type)result->code;
};


Code::Type US::updateFieldByLogin (std::string login, std::string field, std::string value){
  if(!connected) { return Code::US_DBDoesntRespond; }
      
      updateParameters update;
      strcpy(update.login, login.c_str());
      strcpy(update.field, field.c_str());
      strcpy(update.value, value.c_str());
      int32 *result;
      result=updatefieldbylogin_1(&update, client);
      if(!result) { return Code::US_DBDoesntRespond; }

      return (Code::Type)*result;
};


Code::Type US::appendRow (std::string login, std::string password, std::string role, std::string policy, std::string name){
  if(!connected) { return Code::US_DBDoesntRespond; }
      
      appendParameters append;
      strcpy(append.login, login.c_str());
      strcpy(append.password, password.c_str());
      strcpy(append.role, role.c_str());      
      strcpy(append.policy, policy.c_str());      
      strcpy(append.name, name.c_str());
      int32 *result;
      result=appendrow_1(&append, client);
      if(!result) { return Code::US_DBDoesntRespond; }

      return (Code::Type)*result;
};


Code::Type US::deleteRowByLogin (std::string login){
  if(!connected) { return Code::US_DBDoesntRespond; }
      
      deleteParameters delet;
      strcpy(delet.login, login.c_str());

      int32 *result;
      result=deleterowbylogin_1(&delet, client);
      if(!result) { return Code::US_DBDoesntRespond; }

      return (Code::Type)*result;
};

